package com.canbot.u05.activity.reception;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.UsherModeRecordDialog;
import com.canbot.u05.view.UsherModeXFListenerDialog;

/**
 * 文字转语音界面
 */
public class ReceptionModeTextToSpeechActivity extends ReceptionBaseActivity implements View.OnClickListener {

        private ImageView ivBack, btnSure, btnCancel;

        private ImageView ivVoiceIcon;

        private ImageView ivSpeechRecord;

        private EditText editText;

        private TextView tvTitle;

        private UsherModeXFListenerDialog usherModeRecordDialog;

        private ToastTools toastTools;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_reception_mode_text_to_speech);
                toastTools = ToastTools.newInstance(this);
                setViews();
        }

        private void setViews() {
                ivBack = (ImageView) findViewById(R.id.imageView_back);
                tvTitle = (TextView) findViewById(R.id.textView_title);
                btnSure = (ImageView) findViewById(R.id.iv_usher_mode_text_to_speech_imageView_sure);
                btnCancel = (ImageView) findViewById(R.id.iv_usher_mode_text_to_speech_imageView_cancle);
                ivVoiceIcon = (ImageView) findViewById(R.id.iv_usher_mode_text_to_speech_voice_icon);
                ivSpeechRecord = (ImageView) findViewById(R.id.iv_usher_mode_text_to_speech_record_icon);
                editText = (EditText) findViewById(R.id.editText_text_to_speech);
                tvTitle.setText("文字转语音");
                ivBack.setOnClickListener(this);
                btnCancel.setOnClickListener(this);
                btnSure.setOnClickListener(this);
                ivVoiceIcon.setOnClickListener(this);
                ivSpeechRecord.setOnClickListener(this);
                editText.addTextChangedListener(watcher);
                String voiceContent = getIntent().getStringExtra("voiceContent");
                if (!TextUtils.isEmpty(voiceContent)) {
                        editText.setText(voiceContent);
                }
                editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                        @Override
                        public void onFocusChange(View v, boolean hasFocus) {
                                editText.setSelection(editText.length());
                        }
                });
        }

        private TextWatcher watcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.length() > 500) {
                                editText.setText(s.subSequence(0, 500));
                                editText.setSelection(500);
                                toastTools.showText(getString(R.string.input_up_words2));
                        }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.imageView_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.iv_usher_mode_text_to_speech_imageView_sure:
                                //确定
                                String trim = editText.getText().toString().trim();
                                if (TextUtils.isEmpty(trim)) {
                                        toastTools.showText(getString(R.string.text_cannot_empty));
                                        return;
                                }
                                Intent intent = new Intent();
                                intent.putExtra("voiceType", 2);
                                intent.putExtra("voiceContent", trim);
                                setResult(RESULT_OK, intent);
                                finish();
                                break;
                        case R.id.iv_usher_mode_text_to_speech_imageView_cancle:
                                //取消
                                finish();
                                break;
                        case R.id.iv_usher_mode_text_to_speech_voice_icon:
                                //点击声音图标
                                String trim2 = editText.getText().toString().trim();
                                if (TextUtils.isEmpty(trim2)) {
                                        toastTools.showText(getString(R.string.text_cannot_empty));
                                        return;
                                }
                                MsgSendUtils.sendStringMsg(MsgType.PLAY_TTS, trim2);
                                break;
                        case R.id.iv_usher_mode_text_to_speech_record_icon:
                                if (usherModeRecordDialog == null) {
                                        //点击语音录入图标
                                        usherModeRecordDialog = new UsherModeXFListenerDialog(this, UsherModeRecordDialog.USHER_MODE);
                                        usherModeRecordDialog.setFinishClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                        usherModeRecordDialog.ok();
                                                }
                                        });
                                        usherModeRecordDialog.setCancleClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                        usherModeRecordDialog.no();
                                                }
                                        });
                                        setDialogWindowAttr(usherModeRecordDialog, this);
                                }
                                usherModeRecordDialog.show(xunFeiResult);
                                break;
                }
        }

        UsherModeXFListenerDialog.IXunFeiResult xunFeiResult = new UsherModeXFListenerDialog.IXunFeiResult() {
                @Override
                public void onResult(String result) {
                        editText.append(result);
                }

                @Override
                public void onError(int result) {
                        toastTools.showText(getString(R.string.identification_error) + result);
                }
        };

        public static void setDialogWindowAttr(Dialog dlg, Context ctx) {
                Window window = dlg.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                lp.width = (int) ctx.getResources().getDimension(R.dimen.x848);//宽高可设置具体大小
                lp.height = (int) ctx.getResources().getDimension(R.dimen.x499);
                dlg.getWindow().setAttributes(lp);
        }
}
